package com.hifar.demo3;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.InputStream;

public class S1000DValidator {
    public boolean validateAgainstSchema(InputStream xmlStream, File schemaFile) {
        try {
            // 1. 创建Schema工厂
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            
            // 2. 加载Schema
            Source schemaSource = new StreamSource(schemaFile);
            Schema schema = factory.newSchema(schemaSource);
            
            // 3. 创建验证器
            Validator validator = schema.newValidator();
            
            // 4. 验证XML
            Source xmlSource = new StreamSource(xmlStream);
            validator.validate(xmlSource);
            
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}